﻿#version 330 core
//vert
layout(location=0)in vec3 Position;
layout(location=1)in vec3 Normal;
uniform mat4 moduleMat;
layout(std140) uniform SetingBlock
{
	mat4 cameraMat;
};
layout(std140) uniform LightBlock
{
	vec3 lightColor;
	vec3 lightPosition;
	vec3 viewPos;
};

out vec3 LightColor;
out vec3 vNormal;
out vec3 lightDir;
out vec3 ViewDir;

void main()
{
	vNormal = (moduleMat*vec4(Normal,0.0)).xyz;
	LightColor = lightColor;
	vec3 fragPosition = (moduleMat* vec4(Position,1.0)).xyz;
	lightDir = lightPosition-fragPosition;
	ViewDir = viewPos-fragPosition;
	gl_Position = cameraMat*moduleMat*vec4(Position,1.0);
}


//frag
in vec3 LightColor;
in vec3 vNormal;
in vec3 lightDir;
in vec3 ViewDir;
uniform vec3 mainColor;
void main()
{
	vec3 normal = normalize(vNormal);
	vec3 ldir = normalize(lightDir);
	vec3 vdir = normalize(ViewDir);
	vec3 ambient = mainColor*LightColor * 0.2;
	vec3 diffuse = mainColor*LightColor*max(dot(normal,ldir),0);
	vec3 reflectDir = reflect(-ldir, normal);
	vec3 specular = pow(max(dot(vdir, reflectDir), 0.0), 32)*0.5*LightColor;
	gl_FragColor = vec4(ambient+diffuse+specular,1.0);
}